#include "AF_Map.h"


AF_Map::AF_Map(void)
{
    InitGlobalDataFromFile(); //read file once. 
	g_p_graphcut   = new unsigned char[IMG_MAX_SIZE * IMG_MAX_SIZE * 3]; //76800byte = 75kb
	g_p_rough_data = new unsigned char[IMG_MAX_SIZE * IMG_MAX_SIZE * 3]; //76800byte = 75kb
	g_p_AF_data    = new unsigned char[IMG_MAX_SIZE * IMG_MAX_SIZE * 3]; //76800byte = 75kb
	g_p_src_img    = new unsigned char[IMG_MAX_SIZE * IMG_MAX_SIZE * 3]; //76800byte = 75kb
}


AF_Map::~AF_Map(void)
{
	delete[] g_p_graphcut;
	delete[] g_p_rough_data;
	delete[] g_p_AF_data;
	delete[] g_p_src_img;
}


void AF_Map::init(unsigned char* p_src_img, int width, int height)
{

	int size = width * 3 * height;
	for (int i = 0; i < size; i++)
	{
		g_p_graphcut[i]   = 0; //76800byte = 75kb
		g_p_AF_data[i]    = 0; //76800byte = 75kb
		g_p_rough_data[i] = 0; //76800byte = 75kb
	}
	g_img_width  = width;
	g_img_height = height;

    int r, g, b;
    int y, u, v;
    for (int Y = 0; Y < height; Y++) 
	{
        for (int X = 0; X < width; X++)
		{
			int index = Y * width * 3 + X * 3;

			y = (int)p_src_img[index + 0];
			u = (int)p_src_img[index + 1];
			v = (int)p_src_img[index + 2];
            
            u -= 128;
            v -= 128;
            
            r = y + (int)(1.402f*(float)v + 0.5f);
            g = y - (int)(0.344f*(float)u + 0.5f) - (int)(0.714f*(float)v + 0.5f);
            b = y + (int)(1.772f*(float)u + 0.5f);
            
            if (r > 255) r = 255;
            if (r < 0)   r = 0;
            if (g > 255) g = 255;
            if (g < 0)   g = 0;
            if (b > 255) b = 255;
            if (b < 0)   b = 0;
            
            g_p_src_img[index + 0] = b;
            g_p_src_img[index + 1] = g;
            g_p_src_img[index + 2] = r;
        }
    }
}

void AF_Map::run()
{
	int result = SaliencyDetectionForRoc(g_p_src_img, 0, g_img_width, g_img_height, g_p_graphcut, g_p_rough_data, g_p_AF_data);
}